'use strict';

define(['controllers', '../service/replicaService'], function (controllers) {
    controllers.solr.register('ReplicaController', ['$scope', '$rootScope', 'ReplicaService', function ($scope, $rootScope, replicaService) {
        $scope.init = function () {
            $scope.query_host = '';
            $scope.query_state = '';

            $scope.pageNo = 1;
            $scope.pageSize = 10;

            $scope.save_err = false;
        };

        $scope.initEvent = function () {
            $('#editDialog').on('shown.bs.modal', function () {
                if ($scope.save_id != null) {
                } else {
                }
            });
            $('#save_name').keypress(function (e) {
                if (isSubmitKeyCode(e)) {
                }
            });
        };

        $scope.setEditForm = function (replica) {
            replica = replica == null ? {} : replica;
            $scope.save_id = replica.id;
        };

        $scope.query = function (pageNo) {
            if (pageNo != null) {
                $scope.pageNo = pageNo;
            }
            replicaService.query({
                host: $scope.query_host,
                collectionId: $scope.query_collectionId,
                state: $scope.query_state,
                pageNo: $scope.pageNo,
                pageSize: $scope.pageSize
            }, function (data) {
                $scope.paged = data.paged;
                $scope.hosts = data.hosts;
                $scope.states = data.states;
                $scope.collections = data.collections;
                $scope.collectionMap = data.collectionMap;
                $scope.nodeMap = data.nodeMap;
                $scope.shardMap = data.shardMap;
                $scope.monitorMap = data.monitorMap;
            });
        };

        $scope.edit = function (replica) {
            if (replica != null) {
                $scope.setEditForm(replica);
            } else {
                $scope.setEditForm();
            }
            $('#editDialog').modal('show');
        };

        $scope.save = function () {
            $scope.save_err = false;

            replicaService.save({
                id: $scope.save_id
            }, {
            }, function () {
                $('#editDialog').modal('hide');
                $scope.setEditForm(null);
                $scope.query();
            });
        };

        $scope.confirmRemove = function (replica) {
            $scope.removeId = replica.id;
            $('#removeDialog').modal('show');
        };

        $scope.remove = function (id) {
            replicaService.remove({id: $scope.removeId}, function () {
                $scope.removeId = null;
                $('#removeDialog').modal('hide');
                $scope.query();
            });
        };

        $scope.init();
        $scope.initEvent();
        $scope.query();
    }]);
});